home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / ODMemMgr / Sources / PlatfMem.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-04-03  |  5.9 KB  |  227 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PlatfMem.cpp
  3.  
  4.     Contains:    Platform layer implementation
  5.  
  6.     Owned by:    Michael Burbidge
  7.  
  8.     Copyright:    © 1993-96 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.         <10>    03/05/96    mdr        Include Debug.h if MacApp.
  12.          <9>      3/4/96    TWB        Add MMPrintf wrapper around somPrintf. 
  13.          <8>     2/13/96    srf        Build variation for MacApp
  14.  
  15.          <7>      5/4/95    jpa        Leave a min amount of free space in
  16.                                     platform heap. [1235657]
  17.          <6>    10/24/94    jpa        Strip address returned from
  18.                                     PlatformAllocateBlock. [1193659]
  19.          <5>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  20.                                     [1186692]
  21.          <4>     8/17/94    jpa        Zap segments when freed, and check for
  22.                                     errors [1181509]
  23.          <3>      8/8/94    jpa        OD_DEBUG --> ODDebug
  24.          <2>     6/10/94    MB        Make it build
  25.          <1>      6/9/94    MB        first checked in
  26.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  27.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  28.          <1>     4/29/94    MB        first checked in
  29.     To Do:
  30.     In Progress:
  31.         
  32. */
  33.  
  34. #ifndef _PLATFMEM_
  35. #include "PlatfMem.h"
  36. #endif
  37.  
  38. #ifndef _MEMMGRPV_
  39. #include "MemMgrPv.h"
  40. #endif
  41.  
  42. #ifndef __ERRORS__
  43. #include <Errors.h>
  44. #endif
  45.  
  46. #include <string.h>
  47. #include <stdio.h>
  48. #include <stdarg.h>
  49.  
  50. #if defined(qMacApp)
  51. #ifndef __UDEBUG__
  52. #include "UDebug.h"
  53. #endif
  54. #endif
  55.  
  56.  
  57.  
  58.  
  59. //========================================================================================
  60. // External
  61. //========================================================================================
  62.  
  63. /* Adkins -- removed const from first param to somPrintf -- it was never const */
  64. extern "C" int somPrintf (const char * fmt, ...);        // From <somapi.h>
  65.  
  66. //========================================================================================
  67. // Global function definitions
  68. //========================================================================================
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // PlatformZapMem
  72. //----------------------------------------------------------------------------------------
  73.  
  74. void PlatformZapMem( void *start, size_t size, MMULong value )
  75. {
  76.     MM_ASSERT(((size_t)start&3)==0);                // Must start on a 4byte boundary
  77.     long *blk = (long *) start;
  78.     for (long i = size>>2; i>0; i--)                     // (i must be signed!)
  79.         *blk++ = value;
  80.     if( size&3 )
  81.         PlatformCopyMemory(&value,blk, size&3);        // Fill remaining 1-3 bytes
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // PlatformAllocateBlock
  86. //----------------------------------------------------------------------------------------
  87.  
  88. void *PlatformAllocateBlock(size_t size, MMHeapLocation src )
  89. {
  90.     // Check total free space before trying anything; must leave some amount free.
  91.     size_t free;
  92.     MMSystemFreeSpace(src,&free,kMMNULL);
  93.     if( free-size < kPlatformMinFreeSpace )
  94.         return kMMNULL;
  95.         
  96.     Handle handle = (Handle) MMAllocateHandleIn(size,src);
  97.             
  98.     if ( handle ) {
  99. #if !defined(qMacApp)
  100.         // No don't move it high. It will bump against segments when 68k!!
  101.         // We ReserveMem'ed it to allocate it low instead.
  102.         if( src==kMMAppMemory )
  103.             ::MoveHHi(handle);    // Jeff Crawford sez we don't need MoveHHi for temp/sys heap
  104. #endif
  105.         ::HLock(handle);
  106.         OSErr err = MemError();
  107.         
  108.         if (err == noErr)
  109.             return StripAddress(*handle);
  110.         else
  111.             DisposeHandle(handle);
  112.     }
  113.     
  114.     return kMMNULL;
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. // PlatformFreeBlock
  119. //----------------------------------------------------------------------------------------
  120.  
  121. void PlatformFreeBlock(void *ptr)
  122. {
  123.     Handle handle = ::RecoverHandle((Ptr) ptr);
  124.  
  125. #if MM_DEBUG
  126.     OSErr err = MemError();
  127.     if( err )
  128.         MM_WARN("PlatformFreeBlock got err %hd on ptr %p",err,ptr);
  129. #endif
  130.  
  131.     if( handle == NULL )
  132.         return;
  133.     
  134. #if MM_DEBUG
  135.     // Zap the block as we dispose it:
  136.     if( gValidate > 0 )
  137.         PlatformZapMem(ptr,::GetHandleSize(handle),0xDEADBEEF);
  138. #endif
  139.  
  140.     ::DisposeHandle(handle);
  141.     
  142. #if MM_DEBUG
  143.     err = MemError();
  144.     if( err && err != memFullErr )
  145.         MM_WARN("PlatformFreeBlock got err %hd on hdl %p",err,handle);
  146. #endif
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // BREAK
  151. //----------------------------------------------------------------------------------------
  152.  
  153. static void
  154. BREAK( const char msg[] )
  155. {
  156. #if !defined(qMacApp)
  157.     if( gHaveSOM )
  158.         MMPrintf("%s\n",msg);
  159. #endif
  160.     
  161.     // Now convert to Pascal string and call DebugStr:
  162.     Str255 pstr;
  163.     long len = strlen(msg);
  164.     pstr[0] = (len>255) ?255 :len;
  165.     for( int i=pstr[0]; i>0; i-- ) {
  166.         char c = msg[i-1];
  167.         if( c==';' ) c=':';        // ";" is bad in DebugStrs
  168.         pstr[i] = c;
  169.     }
  170.     DebugStr(pstr);
  171. }
  172.  
  173.  
  174. //----------------------------------------------------------------------------------------
  175. // MM_ASSERT_FAILED
  176. //----------------------------------------------------------------------------------------
  177.  
  178. void MM_ASSERT_FAILED(const char *cond, const char *fil)
  179. {
  180.     char dbg[256];
  181.     sprintf(dbg,"MM: %s ...NOT! In %s", cond,fil); 
  182.     BREAK(dbg);
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // MM_SHOW_WARNING
  187. //----------------------------------------------------------------------------------------
  188.  
  189. void MM_SHOW_WARNING(const char *msg, ...)
  190. {
  191.     char buf[512];
  192.     strcpy(buf, "MMWarning: ");
  193.     va_list args;
  194.     va_start(args,msg);
  195.     vsprintf(buf+strlen(buf),msg,args);
  196.     va_end(args);
  197.     
  198.     BREAK(buf);
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // MMPrintf
  203. //----------------------------------------------------------------------------------------
  204.  
  205. int MMPrintf(const char *fmt, ...)
  206. {
  207.     int n;
  208.     char buf[512];
  209.     va_list args;
  210.     va_start(args, fmt);
  211.     vsprintf(buf, fmt, args);
  212.     va_end(args);
  213. #if !defined(qMacApp)
  214.     n = somPrintf(buf);
  215. #else // qMacApp
  216.     n = 0;
  217. #if qDebugMsg
  218.     if (DebugCanWriteLn())
  219.         n = fprintf(stderr, buf);
  220. #endif
  221.     return n;
  222. #endif // qMacApp
  223. }
  224.  
  225. //----------------------------------------------------------------------------------------
  226. // End of PlatfMem.cpp
  227.